include(FindPythonInterp)

find_program(RE2C_BIN re2c)
find_program(GPERF_BIN gperf
  PATHS /media/sed1/32/bin opt/gperf/bin )

set(CMAKE_BUILD_TYPE Debug)

add_custom_command(OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/.keywords.c
  COMMAND ${GPERF_BIN} ${CMAKE_CURRENT_SOURCE_DIR}/keywords
  --lookup-function-name=jscan_is_keyword --omit-struct-type
  --compare-strncmp --struct-type
  --output-file=${CMAKE_CURRENT_SOURCE_DIR}/.keywords.c
  DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/keywords)

add_custom_command(OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/.scanner.c
  COMMAND ${RE2C_BIN} -o ${CMAKE_CURRENT_SOURCE_DIR}/.scanner.c
  -w --no-generation-date
  ${CMAKE_CURRENT_SOURCE_DIR}/scanner.rl  
  DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/scanner.rl)

add_custom_command(OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/.tokenstr.c
  COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/tokenstr.py
		${Jeruk_BINARY_DIR}/parser/parser.h
		${CMAKE_CURRENT_SOURCE_DIR}/.tokenstr.c
		--template ${CMAKE_CURRENT_SOURCE_DIR}/tokenstr.c
  DEPENDS ${Jeruk_BINARY_DIR}/parser/parser.h
  )

add_library(jscan
  ${CMAKE_CURRENT_SOURCE_DIR}/.scanner.c
  ${CMAKE_CURRENT_SOURCE_DIR}/.keywords.c
  ${CMAKE_CURRENT_SOURCE_DIR}/.tokenstr.c)

include_directories(
  ${CMAKE_SOURCE_DIR}/bstrlib
  ${Jeruk_BINARY_DIR}/parser)

add_executable(jscan_test
  test.c)
target_link_libraries(jscan_test
  jscan)